from DataPrepare.dailyFactorsWithInfluxdb.factorBase import factorBase
import cx_Oracle as oracle
import pandas as pd
from Config.myConfig import *
from Config.myConstant import *
from DataAccess.TradedayDataProcess import TradedayDataProcess
########################################################################
class marketData(factorBase):
    """记录日线的行情数据等"""
    #----------------------------------------------------------------------
    def __init__(self):
        #super(buySellVolumeRatio,self).__init__()
        super().__init__()
        self.factor='dailyMarketData'
        pass
    #----------------------------------------------------------------------
    def getDataFromOracleByCode(self,code,startDate,endDate):
        data=self.__getDailyDataByDateFromOracleServer(code,startDate,endDate);
        return data
        pass
    #----------------------------------------------------------------------
    #输入code=600000.SH，startdate=yyyyMMdd，endDate=yyyyMMdd
    def __getDailyDataByDateFromOracleServer(self,code,startDate=EMPTY_STRING,endDate=EMPTY_STRING):
        #获取行情数据
        #附注status -1:交易-2:待核查0:停牌XD:除息XR:除权DR:除权除息N:上市首日
        database='wind_filesync.AShareEODPrices'
        connection = oracle.connect(self.oracleConnectStr)
        cursor = connection.cursor()
        if startDate==EMPTY_STRING:
            oracleStr="select  S_INFO_WINDCODE as code,TRADE_DT as \"date\",S_DQ_OPEN as open,S_DQ_HIGH as high,S_DQ_LOW as low,S_DQ_CLOSE as close,S_DQ_PRECLOSE as preClose,S_DQ_VOLUME as volume,S_DQ_AMOUNT as amount,S_DQ_CHANGE as change,S_DQ_PCTCHANGE as pctChange,S_DQ_ADJFACTOR as adjFactor,S_DQ_AVGPRICE as vwap,S_DQ_TRADESTATUS as status from wind_filesync.AShareEODPrices where S_INFO_WINDCODE='{0}' order by TRADE_DT".format(code)
        elif endDate==EMPTY_STRING:
            oracleStr="select  S_INFO_WINDCODE as code,TRADE_DT as \"date\",S_DQ_OPEN as open,S_DQ_HIGH as high,S_DQ_LOW as low,S_DQ_CLOSE as close,S_DQ_PRECLOSE as preClose,S_DQ_VOLUME as volume,S_DQ_AMOUNT as amount,S_DQ_CHANGE as change,S_DQ_PCTCHANGE as pctChange,S_DQ_ADJFACTOR as adjFactor,S_DQ_AVGPRICE as vwap,S_DQ_TRADESTATUS as status from wind_filesync.AShareEODPrices where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} order by TRADE_DT".format(code,startDate)
        else:
            oracleStr="select  S_INFO_WINDCODE as code,TRADE_DT as \"date\",S_DQ_OPEN as open,S_DQ_HIGH as high,S_DQ_LOW as low,S_DQ_CLOSE as close,S_DQ_PRECLOSE as preClose,S_DQ_VOLUME as volume,S_DQ_AMOUNT as amount,S_DQ_CHANGE as change,S_DQ_PCTCHANGE as pctChange,S_DQ_ADJFACTOR as adjFactor,S_DQ_AVGPRICE as vwap,S_DQ_TRADESTATUS as status from wind_filesync.AShareEODPrices where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} and TRADE_DT<={2} order by TRADE_DT".format(code,startDate,endDate)
        cursor.execute(oracleStr)
        mydata = cursor.fetchall()
        mydata = pd.DataFrame(mydata,columns=['code','date','open','high','low','close','preClose','volume','amount','change','pctChange','adjFactor','vwap','status'])
        mydata[['open','high','low','close','preClose','volume','amount','change','pctChange','adjFactor','vwap']] = mydata[['open','high','low','close','preClose','volume','amount','change','pctChange','adjFactor','vwap']].astype('float')
        mydata['mytime']=pd.to_datetime(mydata['date'],format='%Y%m%d')
        mydata.set_index('mytime',inplace=True,drop=True)
        return mydata  
########################################################################
